<?php
// +----------------------------------------------------------------------
// Csmmeet [ Csmonline ]
// Author: chensm <chenshiming0802@163.com>
// Create by chensm at 2020-03-31
// +----------------------------------------------------------------------
namespace app\admin\controller\csmonline;

use addons\csmonline\library\CsmBackend;
use addons\csmonline\library\CsmonlineCron;

/**
 * 统计图表示例
 *
 * @icon   fa fa-charts
 * @remark 展示在FastAdmin中使用Echarts展示丰富多彩的统计图表
 */
class Charthome extends CsmBackend
{
    protected $model = null;
 
    /**
     * 查看
     */
    public function index()
    {
        //1.0.2 修复了HTTPS无法打开地图的问题@chensm
        //header("Content-Security-Policy: upgrade-insecure-requests");
        //$isrefresh = $this->csmreq("isrefresh", false);
        $today = date("Y-m-d");
        // 是否强制刷新
        if (true) {
            CsmonlineCron::crononedate($today, true);
        }
        $this->view->assign("baseurl", $this->request->baseUrl());
        // 统计截止时间
        if (true) {
            $dao = new \app\admin\model\csmonline\Crontab();
            $row = $dao->where("statdate", "=", $today)->find();
            $this->view->assign("todaystat", ($row == null) ? "今天无数据" : date("Y-m-d H:i:s", $row->starttime));
        }

        $firstid = CsmonlineCron::getUserLogFirstId($today);
        // 在线人数,超过了心跳时间*2，则认为离线（有一次心跳没记录到所以*2，网络延迟所以+10秒）
        if (true) {
            $config = get_addon_config("csmonline");
            $heartseconds = (int) $config["heartseconds"];

            $daolog = new \app\admin\model\csmonline\Userlog();
            $onlinen = $daolog->where("id", ">=", $firstid)
                ->where("isloginuser", "=", "N")
                ->where("durationlasttime", ">", time() - 2 * $heartseconds - 10)
                ->count("distinct userid");
            $onliney = $daolog->where("id", ">=", $firstid)
                ->where("isloginuser", "=", "Y")
                ->where("durationlasttime", ">", time() - 2 * $heartseconds - 10)
                ->count("distinct userid");
            $this->view->assign("onlinen", $onlinen);
            $this->view->assign("onliney", $onliney);
        }

        // 今日新增 和 总注册数
        if (true) {
            $dao = new \app\admin\model\csmonline\Piestatdate();
            $today = $dao->where("statdate", "=", $today)->find();
            $this->view->assign("today", $today);
        }

        return $this->view->fetch();
    }

    public function chart()
    {
        $today = date("Y-m-d");
        $before7 = date("Y-m-d", strtotime("-7 day"));
        $dao = new \app\admin\model\csmonline\Piestathour();
        $online = $dao->where("statdate", "=", $today)
            ->field("concat(stathour,'点') stathour2,uvsn,uvsy")
            ->order("stathour", "asc")
            ->select();

        $dao = new \app\admin\model\csmonline\Piestatdate();
        $pcmobile = $dao->where("statdate", "=", $today)
            ->field("(mobileusy+mobileusn) mobileus,(pcusn+pcusy) pcus")
            ->find();
        if ($pcmobile == null) {
            $pcmobile = [
                0,
                0
            ];
        }

        $dao = new \app\admin\model\csmonline\Piestathour();
        $register = $dao->where("statdate", "=", $today)
            ->field("concat(stathour,'点') stathour2,newregisteredus")
            ->order("stathour", "asc")
            ->select();

        $dao = new \app\admin\model\csmonline\Piestatdate();
        $allregister = $dao->where("statdate", "<=", $today)
            ->where("statdate", ">=", $before7)
            ->field("statdate,allregisteredus")
            ->order("statdate", "asc")
            ->select();

        $this->success('', null, array(
            "online" => $online,
            "pcmobile" => $pcmobile,
            "register" => $register,
            "allregister" => $allregister
        ));
    }

    private function adjust($list)
    {
        $ll = [];
        foreach ($list as $v) {
            $ll[] = $v;
        }
        return $ll;
    }
}
